<template>
  <div class="xtx-goods-page" v-if="goods">
    <!--   v-if="goods" 踩的坑 等到数据加载完成之后在执行否则会标错 -->
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="'/category?id=' + goods.categories[0].id">{{
          goods.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="'/category/sub?id=' + goods.categories[1].id">{{
          goods.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goods.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <div class="media">
          <goods-imgs :images="goods.mainPictures"></goods-imgs>
          <goods-sales></goods-sales>
        </div>
        <div class="spec">
          <!-- 地区选择 -->
          <goods-name :goods="goods"></goods-name>
          <!-- 商品规格 -->
          <goods-sku
            @changeSelect="changeSelect"
            skuId="1369155863389933570"
            :obj="goods"
          ></goods-sku>
          <!-- 小计 -->
          <XtxNumbox label="数量" v-model="num" :max="goods.inventory" />
          <!-- 按钮 -->
          <XtxButton type="primary" style="margin-top:20px;margin-left:20px;"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 热榜 轮播 -->
      <goodsRelevant></goodsRelevant>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs></GoodsTabs>
            <!-- 分页组件 -->
            <xtxPagination></xtxPagination>
          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn></GoodsWarn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :goodsId="goods.id" :type="1" />
          <GoodsHot :goodsId="goods.id" :type="2" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import goodsRelevant from "./components/goodsRelevant.vue";
import { getGoods } from "@/api/goods.js";
import { useRoute } from "vue-router";
import { nextTick, provide,  ref,  watch } from "vue";
import GoodsImgs from "./components/goodsImgs.vue";
import GoodsSales from "./components/goodsSales.vue";
import GoodsName from "./components/goodsName.vue";
import GoodsSku from "./components/goodsSku.vue";
import GoodsTabs from "./components/goodsTabs.vue";
import GoodsHot from "./components/goodsHot.vue";
import GoodsWarn from "./components/goodsWarn.vue";
export default {
  name: "index",
  components: {
    goodsRelevant,
    GoodsImgs,
    GoodsSales,
    GoodsName,
    GoodsSku,
    GoodsTabs,
    GoodsHot,
    GoodsWarn,
  },
  setup() {
    const goods = useGoods();
    // 小计
    const num = ref(1);
    // 商品的选中规格
    function changeSelect(value) {
      console.log(value);
      // 这里是去更改商品的价格 和 库存 因为有些商品的价格是不一样的
      if (value.skuId) {
        goods.value.price = value.price;
        goods.value.oldPrice = value.oldPrice;
        goods.value.inventory = value.inventory;
      }
    }
    // 传子组件
    provide("goods", goods);
    return { goods, changeSelect, num };
  },
};

// 获取商品详情
const useGoods = () => {
  // 出现路由地址商品ID发生变化，但是不会重新初始化组件
  const goods = ref(null);
  const route = useRoute();
  watch(
    () => route.query.id,

    (newVal) => {
      if (newVal && route.path == "/goods/index") {
        getGoods(route.query.id).then((data) => {
          //能在每次更换商品数据的时候，让其他组件每次触发
          goods.value = null;
          nextTick(() => {
            // 让商品数据为null让后使用v-if的组件可以重新销毁和创建
            goods.value = data.result;
          });
        });
      }
    },
    { immediate: true }
  );
  return goods;
};
</script>

<style scoped lang="scss">
// 商品信息
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
